require.config({
    shim:{
        //'swiper':{exports:'swiper'}
    },
    paths : {
        //"vue":"lib/vue",
        "jquery":"lib/jquery",
        "cmd":"cmd"
    }
});

require(['jquery','cmd'],function($){
    
    //get top menu
    var token = detectToken();


    /*获取用户列表START*/
    $.vloading.show();
    /*ajax异步START*/
    $.post(urls.getAccoutlists,{token:localStorage.getItem("token"),pageNow:1,pageSize:10}, 
        function(result){
            $.vloading.remove();
            if(result.code == "1"){
                var getAllAccoutHtml = '';
                for(var i=0;i<result.data.records.length;i++){
                    if(result.data.records[i].locked=="1"){
                        getAllAccoutHtml += '<div class="tableMainEach clearfix"><div data-pid="'+result.data.records[i].parentId+'" data-id="'+result.data.records[i].id+'" data-flag="'+result.data.records[i].locked+'" class="wantXZ table-celA fl"><i data-psd="'+result.data.records[i].password+'" class="xwl i-checkno pdr5"></i></div><div class="table-celB fl">'+result.data.records[i].accountName+'</div><div class="table-celC fl">'+result.data.records[i].userName+'</div><div class="table-celD fl">'+result.data.records[i].total+'</div><div class="table-celE fl">'+result.data.records[i].createTime+'</div><div class="table-celF fl"><div data-flag="'+result.data.records[i].locked+'" data-id="'+result.data.records[i].id+'" class="ifqiyong toggleSwitch toggleListSwitchBtns toggleSwitchIDing"><input type="checkbox" checked /><span class="btn"></span><span class="bg"></span></div></div></div>';
                    }else{
                        getAllAccoutHtml += '<div class="tableMainEach clearfix"><div data-pid="'+result.data.records[i].parentId+'" data-id="'+result.data.records[i].id+'" data-flag="'+result.data.records[i].locked+'" class="wantXZ table-celA fl"><i data-psd="'+result.data.records[i].password+'" class="xwl i-checkno pdr5"></i></div><div class="table-celB fl">'+result.data.records[i].accountName+'</div><div class="table-celC fl">'+result.data.records[i].userName+'</div><div class="table-celD fl">'+result.data.records[i].total+'</div><div class="table-celE fl">'+result.data.records[i].createTime+'</div><div class="table-celF fl"><div data-flag="'+result.data.records[i].locked+'" data-id="'+result.data.records[i].id+'" class="ifqiyong toggleSwitch toggleListSwitchBtns toggleSwitchIDing"><input type="checkbox" /><span class="btn"></span><span class="bg"></span></div></div></div>';
                    }
                }
                $(".tableMainbtom").html(getAllAccoutHtml);
                //总页数
                $(".allpagesNums").html(result.data.pageCount);
                //总记录数
                $(".allRecord").html(result.data.rowCount);
                pageNav.go(1,result.data.pageCount);
                pageNav.fn = function(p,pn){
                    if($(".startSearching").attr("data-status")=="0"){//页面加载时，展示所有账号列表，翻页触发，如果是点击搜索后，不触发
                      getAccoutlistsHtml(p);
                    }
                    
                };
                //启用禁用账户
                var ifkaiqiStatus = 0;
                $(".toggleListSwitchBtns").on("click",function(){
                    var id = $(this).attr("data-id");
                    var curBtnStatusCode = $(this).attr("data-flag");
                    if(ifkaiqiStatus==0){
                        if(curBtnStatusCode==1){
                            //触发右边按钮的同时，更新左边checkbox的data-flag状态码
                            $(this).attr("data-flag","0").parent(".table-celF").siblings(".wantXZ").attr("data-flag","0").find("p").html("0");
                        }else{
                            $(this).attr("data-flag","1").parent(".table-celF").siblings(".wantXZ").attr("data-flag","1").find("p").html("1");
                        }
                        ifkaiqiStatus=1;
                    }else{
                        if(curBtnStatusCode==1){
                            $(this).attr("data-flag","0").parent(".table-celF").siblings(".wantXZ").attr("data-flag","0").find("p").html("0");
                        }else{
                            $(this).attr("data-flag","1").parent(".table-celF").siblings(".wantXZ").attr("data-flag","1").find("p").html("1");
                        }
                        ifkaiqiStatus=0;
                    }
                    window.globalSwitchcode = $(this).attr("data-flag");//important perfect!
                    $.post(urls.disabledAccout,{locked:$(this).attr("data-flag"),token:token,id:id}, 
                        function(result){
                            $.vloading.remove();
                            if(result.code == "1"){
                                $.flytip(result.msg);
                            }else{
                                $.flytip(result.msg);
                            }

                    },"json");
                })
                //勾选账号
                //不管是否有点击右边按钮，都同步通讯至左边checkbox状态码
                $(".wantXZ").on("click",function(){
                    window.globalSwitchcode = $(this).attr("data-flag");//important perfect!
                    window.checkedID = $(this).attr("data-id");//用户id
                    window.checkedPID = $(this).attr("data-pid");//父节点id
                    window.zhanghaoName = $(this).siblings(".table-celB").html();//用户账号名
                    window.zhanghaoNC = $(this).siblings(".table-celC").html();//用户昵称
                    window.zhanghaoPSW = $(this).find("i")[0].attributes[0].nodeValue;//用户psw
                    if($(this).find("i").hasClass("i-checked")){
                        $(this).find("i").removeClass("i-checked").addClass("i-checkno").parent(".wantXZ").parent(".tableMainEach").css("background","#fff").siblings().find(".wantXZ").find("i").removeClass("i-checked").addClass("i-checkno").parent(".wantXZ").parent(".tableMainEach").css("background","#fff");
                    }else{
                        $(this).find("i").removeClass("i-checkno").addClass("i-checked").parent(".wantXZ").parent(".tableMainEach").css("background","#F4F4F4").siblings().find(".wantXZ").find("i").removeClass("i-checked").addClass("i-checkno").parent(".wantXZ").parent(".tableMainEach").css("background","#fff");
                    }
                })

            }else{
                $.flytip(result.msg);
            }

    },"json");
    /*ajax异步END*/
    /*获取用户列表END*/

var getAccoutlistsHtml = function(curPagenums){
    /*获取用户列表START*/
    $.vloading.show();
    /*ajax异步START*/
    $.post(urls.getAccoutlists,{token:localStorage.getItem("token"),pageNow:curPagenums,pageSize:10}, 
        function(result){
            $.vloading.remove();
            if(result.code == "1"){
                var getAllAccoutHtml = '';
                for(var i=0;i<result.data.records.length;i++){
                    if(result.data.records[i].locked=="1"){
                        getAllAccoutHtml += '<div class="tableMainEach clearfix"><div data-pid="'+result.data.records[i].parentId+'" data-id="'+result.data.records[i].id+'" data-flag="'+result.data.records[i].locked+'" class="wantXZ table-celA fl"><i data-psd="'+result.data.records[i].password+'" class="xwl i-checkno pdr5"></i></div><div class="table-celB fl">'+result.data.records[i].accountName+'</div><div class="table-celC fl">'+result.data.records[i].userName+'</div><div class="table-celD fl">'+result.data.records[i].total+'</div><div class="table-celE fl">'+result.data.records[i].createTime+'</div><div class="table-celF fl"><div data-flag="'+result.data.records[i].locked+'" data-id="'+result.data.records[i].id+'" class="ifqiyong toggleSwitch toggleListSwitchBtns toggleSwitchIDing"><input type="checkbox" checked /><span class="btn"></span><span class="bg"></span></div></div></div>';
                    }else{
                        getAllAccoutHtml += '<div class="tableMainEach clearfix"><div data-pid="'+result.data.records[i].parentId+'" data-id="'+result.data.records[i].id+'" data-flag="'+result.data.records[i].locked+'" class="wantXZ table-celA fl"><i data-psd="'+result.data.records[i].password+'" class="xwl i-checkno pdr5"></i></div><div class="table-celB fl">'+result.data.records[i].accountName+'</div><div class="table-celC fl">'+result.data.records[i].userName+'</div><div class="table-celD fl">'+result.data.records[i].total+'</div><div class="table-celE fl">'+result.data.records[i].createTime+'</div><div class="table-celF fl"><div data-flag="'+result.data.records[i].locked+'" data-id="'+result.data.records[i].id+'" class="ifqiyong toggleSwitch toggleListSwitchBtns toggleSwitchIDing"><input type="checkbox" /><span class="btn"></span><span class="bg"></span></div></div></div>';
                    }
                }
                $(".tableMainbtom").html(getAllAccoutHtml);
                //启用禁用账户
                var ifkaiqiStatus = 0;
                $(".toggleListSwitchBtns").on("click",function(){
                    var id = $(this).attr("data-id");
                    var curBtnStatusCode = $(this).attr("data-flag");
                    if(ifkaiqiStatus==0){
                        if(curBtnStatusCode==1){
                            //触发右边按钮的同时，更新左边checkbox的data-flag状态码
                            $(this).attr("data-flag","0").parent(".table-celF").siblings(".wantXZ").attr("data-flag","0").find("p").html("0");
                        }else{
                            $(this).attr("data-flag","1").parent(".table-celF").siblings(".wantXZ").attr("data-flag","1").find("p").html("1");
                        }
                        ifkaiqiStatus=1;
                    }else{
                        if(curBtnStatusCode==1){
                            $(this).attr("data-flag","0").parent(".table-celF").siblings(".wantXZ").attr("data-flag","0").find("p").html("0");
                        }else{
                            $(this).attr("data-flag","1").parent(".table-celF").siblings(".wantXZ").attr("data-flag","1").find("p").html("1");
                        }
                        ifkaiqiStatus=0;
                    }
                    window.globalSwitchcode = $(this).attr("data-flag");//important perfect!
                    $.post(urls.disabledAccout,{locked:$(this).attr("data-flag"),token:localStorage.getItem("token"),id:id}, 
                        function(result){
                            $.vloading.remove();
                            if(result.code == "1"){
                                $.flytip(result.msg);
                            }else{
                                $.flytip(result.msg);
                            }

                    },"json");
                })
                //勾选账号
                //不管是否有点击右边按钮，都同步通讯至左边checkbox状态码
                $(".wantXZ").on("click",function(){
                    window.globalSwitchcode = $(this).attr("data-flag");//important perfect!
                    window.checkedID = $(this).attr("data-id");//当前被勾选了的用户id
                    window.checkedPID = $(this).attr("data-pid");//父节点id
                    window.zhanghaoName = $(this).siblings(".table-celB").html();//用户账号名
                    window.zhanghaoNC = $(this).siblings(".table-celC").html();//用户昵称
                    window.zhanghaoPSW = $(this).find("i")[0].attributes[0].nodeValue;//用户psw
                    if($(this).find("i").hasClass("i-checked")){
                        $(this).find("i").removeClass("i-checked").addClass("i-checkno").parent(".wantXZ").parent(".tableMainEach").css("background","#fff").siblings().find(".wantXZ").find("i").removeClass("i-checked").addClass("i-checkno").parent(".wantXZ").parent(".tableMainEach").css("background","#fff");
                    }else{
                        $(this).find("i").removeClass("i-checkno").addClass("i-checked").parent(".wantXZ").parent(".tableMainEach").css("background","#F4F4F4").siblings().find(".wantXZ").find("i").removeClass("i-checked").addClass("i-checkno").parent(".wantXZ").parent(".tableMainEach").css("background","#fff");
                    }
                    //$(this).find("i").removeClass("i-checkno").addClass("i-checked").parent(".wantXZ").parent(".tableMainEach").css("background","#F4F4F4").siblings().find(".wantXZ").find("i").removeClass("i-checked").addClass("i-checkno").parent(".wantXZ").parent(".tableMainEach").css("background","#fff");
                })

            }else{
                $.flytip(result.msg);
            }

    },"json");
    /*ajax异步END*/
    /*获取用户列表END*/
}



getOnelayerMenu();
getTwolayerMenu(401,402);
$(".mianshowbpt-r a").eq(0).addClass("cur");
getThreelayerMenu(402);

    //新增、编辑、修改个人资料弹层模块
    //判断用户是否有勾选账号用户，若有，允许弹出层

    $(".addNewing,.editing,.fpJurisd,.fpBtns").on("click",function(){
        //获取对应每一个账号的开启禁用状态码
        
        if($(this).attr("data-code")=="2"){
            //新增账号
            var myDate = new Date();
            $(".createdDate").val(myDate.toLocaleDateString()).attr("readOnly","true");
            $(".newaddActLayer,.masking").show();
            $(".layerboxTitleName").html("新增账号");
            disabledBtnStatus();
            //调用全部账号
            //新增账号情况下，默认创建者ID为当前登陆者的ID
            $.post(urls.getAccoutlists,{token:token,pageNow:1,pageSize:5000},
            function(result){
                if(result.code == "1"){
                    var yurauthrHtml = '<option value ="'+localStorage.getItem("dengluzhe_id")+'">'+localStorage.getItem("userName")+'</option>';
                    for(var i=0;i<result.data.records.length;i++){
                        yurauthrHtml +='<option value ="'+result.data.records[i].id+'">'+result.data.records[i].userName+'</option>';
                    }
                    $(".yurauthr").html(yurauthrHtml);
                }else{
                    $.flytip(result.msg);
                }

            },"json");
           
            $("#tijiaoBtn").on("click",function(){
                 //账号的启用与禁用(弹框时,直接获取状态码值即可)
                var locked = $(".toggleLayerSwitchBtns").attr("data-flag");
                var accountName = $(".yurname").val();//账号名（手机号码）
                var password = $(".yurpsw").val();//密码（6未以上数字或字母组合）
                var userName = $(".yurnkn").val();//昵称
                var created_id = $(".yurauthr").val();//从select中读取创建者ID
                
                if(accountName==""){
                    $.flytip("请输入账号");
                    return false;
                }else if(!(/^1[3|4|5|7|8]\d{9}$/.test(accountName))){
                    $.flytip("请输入正确的手机号码");
                    return false;                    
                }else if(password==""){
                    $.flytip("请输入密码");
                    return false;
                }else if(password.length<6){
                    $.flytip("请输入至少6位以上密码");
                    return false;
                }else if(userName==""){
                    $.flytip("请输入昵称");
                    return false;
                }else{
                    $.vloading.show();
                    $.post(urls.regNewAccout,{accountName:accountName,password:password,userName:userName,locked:locked,token:token,created_id:created_id}, 
                        function(result){
                            $.vloading.remove();
                            if(result.code == "1"){
                                $.flytip(result.msg);
                                setTimeout('window.location.reload()',1500);
                            }else{
                                $.flytip(result.msg);
                            }

                    },"json");
                }
  
            })

           
        }else if($(this).attr("data-code")=="3"){
            //编辑账号
            var seconds = document.querySelectorAll(".i-checked");
            if(seconds.length==0){
                $.flytip("请选择一个账号操作");
                return;
            }else{
                var myDate = new Date();
                $(".createdDate").val(myDate.toLocaleDateString());
                $(".editActLayer,.masking").show();
                $(".layerboxTitleName").html("编辑账号");
                $(".yurname2").val(zhanghaoName);//账号名（手机号码）
                // $(".yurpsw2").val(zhanghaoPSW);//密码（6未以上数字或字母组合）
                $(".yurnkn2").val(zhanghaoNC);//昵称
                //调用全部账号
                $.post(urls.getAccoutlists,{token:token,pageNow:1,pageSize:5000}, 
                function(result){
                    if(result.code == "1"){
                        var yurauthrHtml = '';
						
						// 编辑账号增加当前用户选项
						if (checkedPID == localStorage.getItem("dengluzhe_id")){
							yurauthrHtml += '<option child-id="'+localStorage.getItem("dengluzhe_id")+'" value ="'+localStorage.getItem("dengluzhe_id")+'" selected="selected">'+localStorage.getItem("userName")+'</option>';								
						}else{
							yurauthrHtml += '<option child-id="'+localStorage.getItem("dengluzhe_id")+'" value ="'+localStorage.getItem("dengluzhe_id")+'">'+localStorage.getItem("userName")+'</option>';
						}
                        for(var i=0;i<result.data.records.length;i++){
							if (checkedID == result.data.records[i].id)
							{
								continue;
							}
							
                            //查询并且匹配出当前的创建者ID索引
                            //编辑账号情况下，默认创建者ID为当前选中账号的parentId
                            if(checkedPID==result.data.records[i].id){
                                yurauthrHtml +='<option child-id="'+result.data.records[i].id+'" value ="'+result.data.records[i].id+'" selected="selected">'+result.data.records[i].userName+'</option>';
                            }else{
                               yurauthrHtml +='<option child-id="'+result.data.records[i].id+'" value ="'+result.data.records[i].id+'">'+result.data.records[i].userName+'</option>'; 
                            }
                            
                        }
                        $(".yurauthr2").html(yurauthrHtml);
                    }else{
                        $.flytip(result.msg);
                    }

                },"json");
                var locked = window.globalSwitchcode;//important perfect!
                disabledBtnStatus();
                if(locked==1){//账号启用
                    $(".layerqiyong").attr("data-flag","1");
                    $(".layerqiyong").find("input").attr("checked","checked");
                }else{//账号禁用
                    $(".layerqiyong").attr("data-flag","0");
                    $(".layerqiyong").find("input").removeAttr("checked");
                }
                $("#tijiaoBtn-edit").on("click",function(){
                    var accountName = $(".yurname2").val();//账号名（手机号码）
                    var password = $(".yurpsw2").val();//密码（6未以上数字或字母组合）
                    var userName = $(".yurnkn2").val();//昵称
                    var token = localStorage.getItem("token");//登录token
                    var created_id = $(".yurauthr2").val();//创建者ID
                    var id = checkedID;//要修改者ID
                    if(accountName==""){
                        $.flytip("请输入账号");
                        return false;
                    }
                    //if(password==""){
                    //    $.flytip("请输入密码");
                    //    return false;
                    //}else 
                    if(userName==""){
                        $.flytip("请输入昵称");
                        return false;
                    }else{
                        $.vloading.show();
                        var locked = $(".zanshinosee2").attr("data-flag");
                        $.post(urls.editaAccout,{userName:userName,locked:locked,token:token,created_id:created_id,id:id,password:password}, 
                            function(result){
                                $.vloading.remove();
                                if(result.code == "1"){
                                    $.flytip(result.msg);
                                    setTimeout('window.location.reload()',1500);
                                }else{
                                    $.flytip(result.msg);
                                }

                        },"json");
                    }
      
                })
            }


        }else if($(this).attr("data-code")=="4"){
            //确保勾选了一个用户账号
            var seconds = document.querySelectorAll(".i-checked");
            if(seconds.length==0){
                $.flytip("请选择一个账号操作");
                return;
            }else{
                $.vloading.show();
                $(".whatIdentity a").eq(0).addClass("cur").siblings().removeClass("cur");//重置第一个tab
                //判断若是当前登录账号，不可做任何权限的修改，仅可查看
                if(checkedID==localStorage.getItem("dengluzhe_id")){
                    $(".permissBtnsbox").hide();
                }else{
                    $(".permissBtnsbox").show();
                }
                //权限分配模块
                $(".layerBoxing2,.masking").show();
                //分配权限模块开启xiaominger writing awesome默认显示第一个角色权限内容START(一)
                 $.post(urls.checkPermissions,{token:token,groups_id:1,user_id:checkedID},
                    function(result){
                    $.vloading.remove();
                    if(result.code == "1"){
                        var parentA = '';
                        //one layer for
                        for(var i=0;i<result.data.length;i++){
                            var parentB = '';
                            var parentC = '';
                            if(result.data[i].isSelected==1){
                                parentA +='<div class="permissEachbox">\n\
                                    <div class="parentAcontainer"><div data-id="'+result.data[i].id+'" class="menuA"><i class="xwl i-zhedie-off pdr5"></i><input children-nums="'+result.data[i].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].id+'" checked />'+result.data[i].name+'</div></div>\n\
                                    ';                               
                            }else{
                                parentA +='<div class="permissEachbox">\n\
                                    <div class="parentAcontainer"><div data-id="'+result.data[i].id+'" class="menuA"><i class="xwl i-zhedie-off pdr5"></i><input children-nums="'+result.data[i].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].id+'" />'+result.data[i].name+'</div></div>\n\
                                    ';
                            }

                            if(result.data[i].children.length!=0){
                                parentB ='<div class="parentBcontainer">';
                                //two layer for
                                for(var s=0;s<result.data[i].children.length;s++){
                                    if(result.data[i].children[s].isSelected==1){
                                        parentB += '<div class="parentBEach">\n\
                                                <div data-id="'+result.data[i].children[s].id+'" class="menuB"><input children-nums="'+result.data[i].children[s].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].id+'" checked />'+result.data[i].children[s].name+'</div>\n\
                                                ';
                                    }else{
                                        parentB += '<div class="parentBEach">\n\
                                                <div data-id="'+result.data[i].children[s].id+'" class="menuB"><input children-nums="'+result.data[i].children[s].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].id+'" />'+result.data[i].children[s].name+'</div>\n\
                                                ';
                                    }

                                    if(result.data[i].children[s].children.length!=0){
                                        parentC ='<div class="parentCcontainer">';
                                        //three layer for
                                        for(var k=0;k<result.data[i].children[s].children.length;k++){
                                            var parentD = '';
                                            if(result.data[i].children[s].children[k].isSelected==1){
                                                //awesomeST
                                                if(result.data[i].children[s].children[k].children.length!=0){
                                                    parentC +='<span data-id="'+result.data[i].children[s].children[k].id+'" class="parentCEach"><input children-nums="'+result.data[i].children[s].children[k].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].id+'" checked />'+result.data[i].children[s].children[k].name+'<div class="parentDcontainer">';
                                                    for(var m=0;m<result.data[i].children[s].children[k].children.length;m++){
                                                        if(result.data[i].children[s].children[k].children[m].isSelected){
                                                            parentD +='<span data-id="'+result.data[i].children[s].children[k].children[m].id+'" class="parentDEach"><input children-nums="'+result.data[i].children[s].children[k].children[m].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].children[m].id+'" checked />'+result.data[i].children[s].children[k].children[m].btn+'</span>';
                                                        }else{
                                                            parentD +='<span data-id="'+result.data[i].children[s].children[k].children[m].id+'" class="parentDEach"><input children-nums="'+result.data[i].children[s].children[k].children[m].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].children[m].id+'">'+result.data[i].children[s].children[k].children[m].btn+'</span>';
                                                        }
                                                        
                                                    }
                                                    parentC += parentD;
                                                    parentC += '</div></span>';
                                                    //console.log(result.data[i].children[s].children[k].name+" : is more then three layer menu");
                                                }else{
                                                    parentC +='<span data-id="'+result.data[i].children[s].children[k].id+'" class="parentCEach"><input children-nums="'+result.data[i].children[s].children[k].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].id+'" checked />'+result.data[i].children[s].children[k].name+'</span>';
                                                }
                                                //awesomeEd
                                            }else{
                                                //awesomeST
                                                if(result.data[i].children[s].children[k].children.length!=0){
                                                    parentC +='<span data-id="'+result.data[i].children[s].children[k].id+'" class="parentCEach"><input children-nums="'+result.data[i].children[s].children[k].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].id+'" />'+result.data[i].children[s].children[k].name+'<div class="parentDcontainer">';
                                                    for(var m=0;m<result.data[i].children[s].children[k].children.length;m++){
                                                        if(result.data[i].children[s].children[k].children[m].isSelected){
                                                            parentD +='<span data-id="'+result.data[i].children[s].children[k].children[m].id+'" class="parentDEach"><input children-nums="'+result.data[i].children[s].children[k].children[m].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].children[m].id+'" checked />'+result.data[i].children[s].children[k].children[m].btn+'</span>';
                                                        }else{
                                                            parentD +='<span data-id="'+result.data[i].children[s].children[k].children[m].id+'" class="parentDEach"><input children-nums="'+result.data[i].children[s].children[k].children[m].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].children[m].id+'">'+result.data[i].children[s].children[k].children[m].btn+'</span>';
                                                        }
                                                        
                                                    }
                                                    parentC +=parentD;
                                                    parentC += '</div></span>';
                                                    //console.log(result.data[i].children[s].children[k].name+" : is more then three layer menu");
                                                }else{
                                                     parentC +='<span data-id="'+result.data[i].children[s].children[k].id+'" class="parentCEach"><input children-nums="'+result.data[i].children[s].children[k].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].id+'" />'+result.data[i].children[s].children[k].name+'</span>';
                                                }
                                                //awesomeEd
                                            }
                                            
                                        }
                                        parentC +='</div>';
                                        parentB += parentC;
                                    }else{
                                        console.log("only two layer menu");
                                    }
                                    parentB += '</div>';
                                }
                                parentB +='</div>';
                                parentA += parentB;
                            }else{
                                console.log("only one layer menu");
                            }

                           parentA +='</div></div></div></div>'; 
                        }
                        $(".permissionMainbox").html(parentA);
                        $('<div style="height:50px;width:100%;"></div>').appendTo($(".parentDcontainer").parent(".parentCEach").parent(".parentCcontainer").parent(".parentBEach"));
                    //一系列事件START
                    //树形结构面板点击切换效果
                     $(".parentAcontainer").click(function(){
                        //$(this).siblings(".parentBcontainer").toggle(200);
                        $(this).siblings(".parentBcontainer").show(200).parent(".permissEachbox").siblings().find(".parentBcontainer").hide(200);
                        $(this).find("i").removeClass("i-zhedie-off").addClass("i-zhedie-on").parent(".menuA").parent(".parentAcontainer").parent(".permissEachbox").siblings().find(".parentAcontainer").find(".menuA").find("i").removeClass("i-zhedie-on").addClass("i-zhedie-off");
                        
                     })
                     //树形结构关系关联勾选
                     //遍历所有初始CheckBox的ID并存储拷贝
                     var checkboxPionts = document.querySelectorAll(".checkboxPiont");
                     var checkboxPiont_Arry = [];
                     for(var i = 0, j = checkboxPionts.length; i < j; i++){
                        //checkboxPiont_Arry.push(checkboxPionts[i].defaultValue);
                        checkboxPiont_Arry.push($(".checkboxPiont").eq(i).val());
                     }
                     $(".checkboxPiont").unbind("click").click(function(){
                        var index_id = $(this).val();
                        var index_key = checkboxPiont_Arry.indexOf(index_id);
                        if($(this).attr("children-nums")!=0){//如果该节点含有子节点
                            if(!($(this).is(':checked'))){
                                if($(this).parent().hasClass("menuA")){//A级别主菜单取消
                                    $(this).parent().parent().siblings().find("input").attr("checked", false);
                                }if($(this).parent().hasClass("menuB")){//B级别主菜单取消
                                    $(this).parent().siblings().find("input").attr("checked", false);
                                }if($(this).parent().hasClass("parentCEach")){//C级别主菜单取消
                                    $(this).siblings(".parentDcontainer").find(".parentDEach input").attr("checked", false);
                                }
                            }else{
                                if($(this).parent().hasClass("menuA")){//A级别主菜单选中
                                    $(this).parent().parent().siblings(".parentBcontainer").find(".parentBEach").find("input").attr("checked", true);
                                }if($(this).parent().hasClass("menuB")){//B级别主菜单选中
                                    $(this).parent().siblings().find("input").attr("checked", true);
                                }if($(this).parent().hasClass("parentCEach")){//C级别主菜单选中
                                    $(this).siblings(".parentDcontainer").find(".parentDEach input").attr("checked", true);
                                }                                
                            }
                        }else{//如果该节点不含有子节点
                            if(!($(this).is(':checked'))){
                                //console.log("had canceled id:"+index_id+",at:"+index_key);
                                checkboxPiont_Arry.splice(index_key,1);
                            }else{
                                //console.log("had choosed id:"+index_id+",at:"+index_key);
                                checkboxPiont_Arry.push(index_id);
                            }
                        }
                        //不论某节点是否含有子节点，当该子节点选中，对应向上父节点也自动勾选
                        if($(this).is(':checked')){
                            if($(this).parent().parent().siblings().hasClass("menuB")){//处于B级别下面子节点
                                $(this).parent().parent().siblings().find("input").attr("checked", true).parent().parent().parent().siblings().find(".menuA").find("input").attr("checked", true);
                            }if($(this).parent().hasClass("menuB")){//处于B级别子节点
                                $(this).parent().parent().parent().siblings().find("input").attr("checked", true);
                            }
                            
                        }
                        
                     })

                    //一系列事件END                      

                   }
                },"json");
                //分配权限模块开启xiaominger writing awesome默认显示第一个角色权限内容END(一)
                    //点击根据角色分别显示对应权限内容START
                    $(".whatIdentity a").unbind("click").click(function(){
                        var identity_id = $(this).attr("identity-id");
                        $(this).addClass("cur").siblings().removeClass("cur");
                        $.ajax({ 
                            type:"post", 
                            url:urls.checkPermissions, 
                            //data: {token:token,groups_id:identity_id,user_id:checkedID},
                            data: {token:token,groups_id:identity_id},
                            dataType: 'json', 
                            async : true,//同步，操作全局变量赋值成功 
                            success:function(result){
                                var parentA = '';
                                //one layer for
                                for(var i=0;i<result.data.length;i++){
                                    var parentB = '';
                                    var parentC = '';
                                    if(result.data[i].isSelected==1){
                                        parentA +='<div class="permissEachbox">\n\
                                            <div class="parentAcontainer"><div data-id="'+result.data[i].id+'" class="menuA"><i class="xwl i-zhedie-off pdr5"></i><input children-nums="'+result.data[i].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].id+'" checked />'+result.data[i].name+'</div></div>\n\
                                            ';                               
                                    }else{
                                        parentA +='<div class="permissEachbox">\n\
                                            <div class="parentAcontainer"><div data-id="'+result.data[i].id+'" class="menuA"><i class="xwl i-zhedie-off pdr5"></i><input children-nums="'+result.data[i].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].id+'" />'+result.data[i].name+'</div></div>\n\
                                            ';
                                    }

                                    if(result.data[i].children.length!=0){
                                        parentB ='<div class="parentBcontainer">';
                                        //two layer for
                                        for(var s=0;s<result.data[i].children.length;s++){
                                            if(result.data[i].children[s].isSelected==1){
                                                parentB += '<div class="parentBEach">\n\
                                                        <div data-id="'+result.data[i].children[s].id+'" class="menuB"><input children-nums="'+result.data[i].children[s].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].id+'" checked />'+result.data[i].children[s].name+'</div>\n\
                                                        ';
                                            }else{
                                                parentB += '<div class="parentBEach">\n\
                                                        <div data-id="'+result.data[i].children[s].id+'" class="menuB"><input children-nums="'+result.data[i].children[s].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].id+'" />'+result.data[i].children[s].name+'</div>\n\
                                                        ';
                                            }

                                            if(result.data[i].children[s].children.length!=0){
                                                parentC ='<div class="parentCcontainer">';
                                                //three layer for
                                                for(var k=0;k<result.data[i].children[s].children.length;k++){
                                                    var parentD = '';
                                                    if(result.data[i].children[s].children[k].isSelected==1){
                                                        //awesomeST
                                                        if(result.data[i].children[s].children[k].children.length!=0){
                                                            parentC +='<span data-id="'+result.data[i].children[s].children[k].id+'" class="parentCEach"><input children-nums="'+result.data[i].children[s].children[k].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].id+'" checked />'+result.data[i].children[s].children[k].name+'<div class="parentDcontainer">';
                                                            for(var m=0;m<result.data[i].children[s].children[k].children.length;m++){
                                                                if(result.data[i].children[s].children[k].children[m].isSelected){
                                                                    parentD +='<span data-id="'+result.data[i].children[s].children[k].children[m].id+'" class="parentDEach"><input children-nums="'+result.data[i].children[s].children[k].children[m].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].children[m].id+'" checked />'+result.data[i].children[s].children[k].children[m].btn+'</span>';
                                                                }else{
                                                                    parentD +='<span data-id="'+result.data[i].children[s].children[k].children[m].id+'" class="parentDEach"><input children-nums="'+result.data[i].children[s].children[k].children[m].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].children[m].id+'">'+result.data[i].children[s].children[k].children[m].btn+'</span>';
                                                                }
                                                                
                                                            }
                                                            parentC += parentD;
                                                            parentC += '</div></span>';
                                                            //console.log(result.data[i].children[s].children[k].name+" : is more then three layer menu");
                                                        }else{
                                                            parentC +='<span data-id="'+result.data[i].children[s].children[k].id+'" class="parentCEach"><input children-nums="'+result.data[i].children[s].children[k].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].id+'" checked />'+result.data[i].children[s].children[k].name+'</span>';
                                                        }
                                                        //awesomeEd
                                                    }else{
                                                        //awesomeST
                                                        if(result.data[i].children[s].children[k].children.length!=0){
                                                            parentC +='<span data-id="'+result.data[i].children[s].children[k].id+'" class="parentCEach"><input children-nums="'+result.data[i].children[s].children[k].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].id+'" />'+result.data[i].children[s].children[k].name+'<div class="parentDcontainer">';
                                                            for(var m=0;m<result.data[i].children[s].children[k].children.length;m++){
                                                                if(result.data[i].children[s].children[k].children[m].isSelected){
                                                                    parentD +='<span data-id="'+result.data[i].children[s].children[k].children[m].id+'" class="parentDEach"><input children-nums="'+result.data[i].children[s].children[k].children[m].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].children[m].id+'" checked />'+result.data[i].children[s].children[k].children[m].btn+'</span>';
                                                                }else{
                                                                    parentD +='<span data-id="'+result.data[i].children[s].children[k].children[m].id+'" class="parentDEach"><input children-nums="'+result.data[i].children[s].children[k].children[m].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].children[m].id+'">'+result.data[i].children[s].children[k].children[m].btn+'</span>';
                                                                }
                                                                
                                                            }
                                                            parentC +=parentD;
                                                            parentC += '</div></span>';
                                                            //console.log(result.data[i].children[s].children[k].name+" : is more then three layer menu");
                                                        }else{
                                                             parentC +='<span data-id="'+result.data[i].children[s].children[k].id+'" class="parentCEach"><input children-nums="'+result.data[i].children[s].children[k].children.length+'" class="checkboxPiont" name="Awesomzd" type="checkbox" value="'+result.data[i].children[s].children[k].id+'" />'+result.data[i].children[s].children[k].name+'</span>';
                                                        }
                                                        //awesomeEd
                                                    }
                                                    
                                                }
                                                parentC +='</div>';
                                                parentB += parentC;
                                            }else{
                                                console.log("only two layer menu");
                                            }
                                            parentB += '</div>';
                                        }
                                        parentB +='</div>';
                                        parentA += parentB;
                                    }else{
                                        console.log("only one layer menu");
                                    }

                                   parentA +='</div></div></div></div>'; 
                                }
                                $(".permissionMainbox").html(parentA);
                                $('<div style="height:50px;width:100%;"></div>').appendTo($(".parentDcontainer").parent(".parentCEach").parent(".parentCcontainer").parent(".parentBEach"));
                              //一系列事件START
                            //树形结构面板点击切换效果
                             $(".parentAcontainer").click(function(){
                                //$(this).siblings(".parentBcontainer").toggle(200);
                                $(this).siblings(".parentBcontainer").show(200).parent(".permissEachbox").siblings().find(".parentBcontainer").hide(200);
                                $(this).find("i").removeClass("i-zhedie-off").addClass("i-zhedie-on").parent(".menuA").parent(".parentAcontainer").parent(".permissEachbox").siblings().find(".parentAcontainer").find(".menuA").find("i").removeClass("i-zhedie-on").addClass("i-zhedie-off");
                                
                             })
                             //树形结构关系关联勾选
                             //遍历所有初始CheckBox的ID并存储拷贝
                             var checkboxPionts = document.querySelectorAll(".checkboxPiont");
                             var checkboxPiont_Arry = [];
                             for(var i = 0, j = checkboxPionts.length; i < j; i++){
                                //checkboxPiont_Arry.push(checkboxPionts[i].defaultValue);
                                checkboxPiont_Arry.push($(".checkboxPiont").eq(i).val());
                             }
                             $(".checkboxPiont").unbind("click").click(function(){
                                var index_id = $(this).val();
                                var index_key = checkboxPiont_Arry.indexOf(index_id);
                                if($(this).attr("children-nums")!=0){//如果该节点含有子节点
                                    if(!($(this).is(':checked'))){
                                        if($(this).parent().hasClass("menuA")){//A级别主菜单取消
                                            $(this).parent().parent().siblings().find("input").attr("checked", false);
                                        }if($(this).parent().hasClass("menuB")){//B级别主菜单取消
                                            $(this).parent().siblings().find("input").attr("checked", false);
                                        }if($(this).parent().hasClass("parentCEach")){//C级别主菜单取消
                                            $(this).siblings(".parentDcontainer").find(".parentDEach input").attr("checked", false);
                                        }
                                    }else{
                                        if($(this).parent().hasClass("menuA")){//A级别主菜单取消
                                            $(this).parent().parent().siblings().find("input").attr("checked", true);
                                        }if($(this).parent().hasClass("menuB")){//B级别主菜单取消
                                            $(this).parent().siblings().find("input").attr("checked", true);
                                        }if($(this).parent().hasClass("parentCEach")){//C级别主菜单选中
                                            $(this).siblings(".parentDcontainer").find(".parentDEach input").attr("checked", true);
                                        }                                 
                                    }
                                }else{//如果该节点不含有子节点
                                    if(!($(this).is(':checked'))){
                                        //console.log("had canceled id:"+index_id+",at:"+index_key);
                                        checkboxPiont_Arry.splice(index_key,1);
                                    }else{
                                        //console.log("had choosed id:"+index_id+",at:"+index_key);
                                        checkboxPiont_Arry.push(index_id);
                                    }
                                }
                                //不论某节点是否含有子节点，当该子节点选中，对应向上父节点也自动勾选
                                if($(this).is(':checked')){
                                    if($(this).parent().parent().siblings().hasClass("menuB")){//处于B级别下面子节点
                                        $(this).parent().parent().siblings().find("input").attr("checked", true).parent().parent().parent().siblings().find(".menuA").find("input").attr("checked", true);
                                    }if($(this).parent().hasClass("menuB")){//处于B级别子节点
                                        $(this).parent().parent().parent().siblings().find("input").attr("checked", true);
                                    }
                                    
                                }
                                
                             })
                            //一系列事件END                                
                            } 
                        });
                        
                    })
                //点击根据角色分别显示对应权限内容END
                //保存提交START
                $(".saveChangebtns").unbind("click").click(function(){
                    //统计最终用户勾选了的CheckBox id集合
                    var checkboxPionts = document.querySelectorAll(".checkboxPiont");
                    var checkboxPiont_Arry = [];
                    console.log(checkboxPionts.length);
                    for(var i = 0, j = checkboxPionts.length; i < j; i++){
                        //console.log(checkboxPionts[i].checked);//判断checkbox是否被选中
                        if(checkboxPionts[i].checked){
                            //checkboxPiont_Arry.push(checkboxPionts[i].defaultValue);
                            checkboxPiont_Arry.push($(".checkboxPiont").eq(i).val());
                        }
                     }
                     console.log(checkboxPiont_Arry.join(","));
                     resId = checkboxPiont_Arry.join(",");
                       $.ajax({ 
                            type:"post", 
                            url:urls.changePermissions, 
                            data: {userId:checkedID,resId:resId,token:token},
                            dataType: 'json', 
                            async : true,//同步，操作全局变量赋值成功 
                            success:function(result){
                                $.flytip(result.msg);
                                $(".layerBoxing2,.masking").fadeOut();
                            } 
                        });
                 })
                //保存提交START
                //取消
                $(".cancelChangebtns").unbind("click").click(function(){
                    $(".layerBoxing2,.masking").fadeOut();
                })

            }

            
        }//data-code==4 END
        else if($(this).attr("data-code")=="5"){//设备分配START
            var seconds = document.querySelectorAll(".i-checked");
            if(seconds.length==0){
                $.flytip("请选择一个账号操作");
                return;
            }else{
                // ==========设备分配START==========
                //判断若是当前登录账号，不可接触和分配设备，未关联设备也不可见
                if(checkedID==localStorage.getItem("dengluzhe_id")){
                    $(".deviceAltEach_r,.FenpeiBtn_box").hide();
                    $(".deviceAltEach_l").css("marginLeft","253px");
                }else{
                    $(".deviceAltEach_r,.FenpeiBtn_box").show();
                    $(".deviceAltEach_l").css("marginLeft","0");
                }
                $(".deviceAllocationLayer,.masking").show();
                $(".showYFPHml").html("");
                $(".showWFPHml").html("");
                var getYFPDevices = function(pageNow,YFPSearchVal){
                   $.ajax({ 
                        type:"post", 
                        url:urls.getDeviceYfp, 
                        data: {token:token,name:YFPSearchVal,id:checkedID,pageNow:pageNow,pageSize:30}, 
                        dataType: 'json', 
                        async : true,
                        success:function(result){ 
                               var YFPHtml = '';
                               for(var i=0;i<result.data.records.length;i++){
                                    if(result.data.records[i].switch==1){//1开启
                                        YFPHtml +='<div class="deviceAltlistEach clearfix JCPoint">\n\
                                                <i data-dtu="'+result.data.records[i].dtu_id+'" class="xwl i-checkno fl"></i>\n\
                                                <span class="act_code fl">'+result.data.records[i].device_no+'</span>\n\
                                                <span class="bh_code fl">'+result.data.records[i].dtu_id+'</span>\n\
                                                <span class="kh_code fl">'+result.data.records[i].customer_name+'</span>\n\
                                                <div data-flag="1" data-dtu="'+result.data.records[i].dtu_id+'" class="toggleSwitch toggleSwitchIDing"><input type="checkbox" checked><span class="btn"></span><span class="bg"></span></div>\n\
                                            </div>';
                                    }else{//0关闭
                                        YFPHtml +='<div class="deviceAltlistEach clearfix JCPoint">\n\
                                                <i data-dtu="'+result.data.records[i].dtu_id+'" class="xwl i-checkno fl"></i>\n\
                                                <span class="act_code fl">'+result.data.records[i].device_no+'</span>\n\
                                                <span class="bh_code fl">'+result.data.records[i].dtu_id+'</span>\n\
                                                <span class="kh_code fl">'+result.data.records[i].customer_name+'</span>\n\
                                                <div data-flag="0" data-dtu="'+result.data.records[i].dtu_id+'" class="toggleSwitch toggleSwitchIDing"><input type="checkbox"><span class="btn"></span><span class="bg"></span></div>\n\
                                            </div>';
                                    }
                               }
                               $(".showYFPHml").append(YFPHtml);
                               //=====开关效果及状态改变START=====
                                var ifkaiqiStatus = 0;
                                $(".toggleSwitch").unbind("click").click(function(){
                                    var curBtnStatusCode = $(this).attr("data-flag");
                                    if(ifkaiqiStatus==0){
                                        if(curBtnStatusCode==1){
                                            $(this).attr("data-flag","0");
                                        }else{
                                            $(this).attr("data-flag","1");
                                        }
                                        ifkaiqiStatus=1;
                                    }else{
                                        if(curBtnStatusCode==1){
                                            $(this).attr("data-flag","0");
                                        }else{
                                            $(this).attr("data-flag","1");
                                        }
                                        ifkaiqiStatus=0;
                                    }
                                    
                                    //======短信通知开关START======
                                    $.ajax({ 
                                        type:"post", 
                                        url:urls.onORoffmsg, 
                                        data: {user_id:checkedID,dtu_id:$(this).attr("data-dtu"),token:token,switch:$(this).attr("data-flag")}, 
                                        dataType: 'json', 
                                        async : true,
                                        success:function(result){ 
                                            $.flytip(result.msg);
                                        } 
                                    });
                                    //======短信通知开关END======
                        
                                    })
                 
                                    //=====开关效果及状态改变END===== 
                                    
                                    //已经关联设备DOM单勾选批量勾选START
                                    $(".JCPoint i").unbind("click").click(function(){
                                        if($(this).hasClass("i-checkno")){
                                            $(this).removeClass("i-checkno").addClass("i-checked");
                                        }else{
                                            $(this).removeClass("i-checked").addClass("i-checkno");
                                        }
                                    })
                                    //已经关联设备DOM单勾选批量勾选START
                                    
                                    //已经关联设备DOM---设备分配（可批量处理）START
                                    $(".fastJC").unbind("click").click(function(){
                                        //从已勾选好的checkbox中获取dtu_id集合
                                        var dtu_idArray =[];
                                        var dtu_idLengths = document.querySelectorAll(".JCPoint .i-checked");
                                        for(var i = 0, j = dtu_idLengths.length; i < j; i++){
                                             //dtu_idArray.push(dtu_idLengths[i].attributes[0].value);
                                             dtu_idArray.push($(".JCPoint .i-checked").eq(i).attr("data-dtu"));
                                         }
                                        //console.log(dtu_idArray.join(","));
                                        if(dtu_idLengths.length==0){
                                            $.flytip("请选择设备");
                                            return false;
                                        }
                                        $.ajax({ 
                                            type:"post", 
                                            url:urls.unbindDevices, 
                                            data: {token:token,user_id:checkedID,dtu_id:dtu_idArray.join(",")}, 
                                            dataType: 'json', 
                                            async : true,
                                            success:function(result){ 
                                                 $.flytip(result.msg);
                                                 if($(".JCPoint").find("i").hasClass("i-checked")){
                                                    $(".i-checked").parent(".JCPoint").remove();
                                                    $(".showWFPHml").html("");
                                                    getWFPDevices(1);
                                                 }
                                            } 
                                        });

                                    })
                                    //已经关联设备DOM---设备分配（可批量处理）END



                               
                        } 
                    }); 
                }
                getYFPDevices(1);    
                // ==========获取用户已分配设备 END ==========
                // ==========获取用户未分配设备START==========
                var getWFPDevices = function(pageNow,WFPSearchVal){
                   $.ajax({ 
                        type:"post", 
                        url:urls.getDeviceWFP, 
                        data: {token:token,name:WFPSearchVal,id:checkedID,pageNow:pageNow,pageSize:30}, 
                        dataType: 'json', 
                        async : true,
                        success:function(result){ 
                           var WFPHtml = '';
                           for(var i=0;i<result.data.records.length;i++){
                               WFPHtml +='<div class="deviceAltlistEach clearfix FPPoint">\n\
                                        <i data-dtu="'+result.data.records[i].dtu_id+'" class="xwl i-checkno fl"></i>\n\
                                        <span class="act_code fl">'+result.data.records[i].device_no+'</span>\n\
                                        <span class="bh_code fl">'+result.data.records[i].dtu_id+'</span>\n\
                                        <span class="kh_code fl">'+result.data.records[i].customer_name+'</span>\n\
                                    </div>';
                                
                           }
                           $(".showWFPHml").append(WFPHtml);
                            //未关联设备DOM单勾选批量勾选START
                            $(".FPPoint i").unbind("click").click(function(){
                                if($(this).hasClass("i-checkno")){
                                    $(this).removeClass("i-checkno").addClass("i-checked");
                                }else{
                                    $(this).removeClass("i-checked").addClass("i-checkno");
                                }
                            })
                            //未关联设备DOM单勾选批量勾选END
                            
                            //未关联设备DOM---设备分配（可批量处理）START
                            $(".fastFP").unbind("click").click(function(){
                                //从已勾选好的checkbox中获取dtu_id集合
                                var dtu_idArray =[];
                                var dtu_idLengths = document.querySelectorAll(".FPPoint .i-checked");
                                for(var i = 0, j = dtu_idLengths.length; i < j; i++){
                                    //dtu_idArray.push(dtu_idLengths[i].attributes[0].value);
                                    dtu_idArray.push($(".FPPoint .i-checked").eq(i).attr("data-dtu"));
                                 }
                                //console.log(dtu_idArray.join(","));
                                if(dtu_idLengths.length==0){
                                            $.flytip("请选择设备");
                                            return false;
                                        }
                                $.ajax({ 
                                    type:"post", 
                                    url:urls.bindingDevices, 
                                    data: {token:token,user_id:checkedID,dtu_id:dtu_idArray.join(",")}, 
                                    dataType: 'json', 
                                    async : true,
                                    success:function(result){ 
                                         $.flytip(result.msg);
                                         if($(".FPPoint").find("i").hasClass("i-checked")){
                                            $(".i-checked").parent(".FPPoint").remove();
                                            $(".showYFPHml").html("");
                                            getYFPDevices(1);
                                         }
                                    } 
                                });

                            })
                            //未关联设备DOM---设备分配（可批量处理）END
                               

                        } 
                    }); 
                }
                getWFPDevices(1); 
                // ==========获取用户未分配设备 END ==========
                //搜索已分配设备和未关联设备START
                $(".YFPSearchBtnBtn").unbind("click").click(function(){
                    $(".showYFPHml").html("");
                    var YFPSearchVal = $(".YFPSearch").val(); 
                    getYFPDevices(1,YFPSearchVal);
                })
                $(".WFPSearchBtnBtn").unbind("click").click(function(){
                    $(".showWFPHml").html("");
                    var WFPSearchVal = $(".WFPSearch").val();
                    getWFPDevices(1,WFPSearchVal);
                })
                //搜索已分配设备和未关联设备END
                //处理分页START
                var pageNowings_a=1;
                $(".showYFPHml").scroll(function(){
                    var h = $(this).height();
                    var sh = $(this)[0].scrollHeight;
                    var st =$(this)[0].scrollTop;
                    if(h+st>=sh){
                        var YFPSearchVal = $(".YFPSearch").val();
                        pageNowings_a++;
                        getYFPDevices(pageNowings_a,YFPSearchVal);
                    }
                })
                var pageNowings_b=1;
                $(".showWFPHml").scroll(function(){
                    var h = $(this).height();
                    var sh = $(this)[0].scrollHeight;
                    var st =$(this)[0].scrollTop;
                    if(h+st>=sh){
                        pageNowings_b++;
                        var WFPSearchVal = $(".WFPSearch").val();
                        getWFPDevices(pageNowings_b,WFPSearchVal);
                    }
                })
                //处理分页END



                // ==========设备分配 END ==========
            }

        }


    });

    
    //点击搜索
    $(".startSearching").on("click",function(){
        var name = $(".searchactName").val();
        $.vloading.show();
        $.ajax({ 
            type:"post", 
            url:urls.getAccoutlists, 
            data: {token:token,name:name,pageNow:1,pageSize:10}, 
            dataType: 'json', 
            async : true,
            success:function(result){
                $.vloading.remove();
                $(".startSearching").attr("data-status","1"); 
                if(result.data.records.length==0){
                   $(".tableMainbtom").html('<p class="txtc" style="padding:19px 0;font-size:16px;border-right:1px solid #e9e9e9;border-bottom:1px solid #e9e9e9;">暂无数据</p>'); 
               }else{
                    var getAllAccoutHtml = '';
                    for(var i=0;i<result.data.records.length;i++){
                        if(result.data.records[i].locked=="1"){
                            getAllAccoutHtml += '<div class="tableMainEach clearfix"><div data-pid="'+result.data.records[i].parentId+'" data-id="'+result.data.records[i].id+'" data-flag="'+result.data.records[i].locked+'" class="wantXZ table-celA fl"><i data-psd="'+result.data.records[i].password+'" class="xwl i-checkno pdr5"></i></div><div class="table-celB fl">'+result.data.records[i].accountName+'</div><div class="table-celC fl">'+result.data.records[i].userName+'</div><div class="table-celD fl">'+result.data.records[i].total+'</div><div class="table-celE fl">'+result.data.records[i].createTime+'</div><div class="table-celF fl"><div data-flag="'+result.data.records[i].locked+'" data-id="'+result.data.records[i].id+'" class="ifqiyong toggleSwitch toggleListSwitchBtns toggleSwitchIDing"><input type="checkbox" checked /><span class="btn"></span><span class="bg"></span></div></div></div>';
                        }else{
                            getAllAccoutHtml += '<div class="tableMainEach clearfix"><div data-pid="'+result.data.records[i].parentId+'" data-id="'+result.data.records[i].id+'" data-flag="'+result.data.records[i].locked+'" class="wantXZ table-celA fl"><i data-psd="'+result.data.records[i].password+'" class="xwl i-checkno pdr5"></i></div><div class="table-celB fl">'+result.data.records[i].accountName+'</div><div class="table-celC fl">'+result.data.records[i].userName+'</div><div class="table-celD fl">'+result.data.records[i].total+'</div><div class="table-celE fl">'+result.data.records[i].createTime+'</div><div class="table-celF fl"><div data-flag="'+result.data.records[i].locked+'" data-id="'+result.data.records[i].id+'" class="ifqiyong toggleSwitch toggleListSwitchBtns toggleSwitchIDing"><input type="checkbox" /><span class="btn"></span><span class="bg"></span></div></div></div>';
                        }
                    }
                    $(".tableMainbtom").html(getAllAccoutHtml);                
                }
                //总页数
                $(".allpagesNums").html(result.data.pageCount);
                //总记录数
                $(".allRecord").html(result.data.rowCount);
                pageNav.go(1,result.data.pageCount);
                pageNav.fn = function(p,pn){
                    clickSearching(p);
                };

                //用户禁用与前面CheckBox勾选START
                //启用禁用账户
                var ifkaiqiStatus = 0;
                $(".toggleListSwitchBtns").on("click",function(){
                    var id = $(this).attr("data-id");
                    var curBtnStatusCode = $(this).attr("data-flag");
                    if(ifkaiqiStatus==0){
                        if(curBtnStatusCode==1){
                            //触发右边按钮的同时，更新左边checkbox的data-flag状态码
                            $(this).attr("data-flag","0").parent(".table-celF").siblings(".wantXZ").attr("data-flag","0").find("p").html("0");
                        }else{
                            $(this).attr("data-flag","1").parent(".table-celF").siblings(".wantXZ").attr("data-flag","1").find("p").html("1");
                        }
                        ifkaiqiStatus=1;
                    }else{
                        if(curBtnStatusCode==1){
                            $(this).attr("data-flag","0").parent(".table-celF").siblings(".wantXZ").attr("data-flag","0").find("p").html("0");
                        }else{
                            $(this).attr("data-flag","1").parent(".table-celF").siblings(".wantXZ").attr("data-flag","1").find("p").html("1");
                        }
                        ifkaiqiStatus=0;
                    }
                    window.globalSwitchcode = $(this).attr("data-flag");//important perfect!
                    $.post(urls.disabledAccout,{locked:$(this).attr("data-flag"),token:token,id:id}, 
                        function(result){
                            $.vloading.remove();
                            if(result.code == "1"){
                                $.flytip(result.msg);
                            }else{
                                $.flytip(result.msg);
                            }

                    },"json");
                })
                //勾选账号
                //不管是否有点击右边按钮，都同步通讯至左边checkbox状态码
                $(".wantXZ").on("click",function(){
                    window.globalSwitchcode = $(this).attr("data-flag");//important perfect!
                    window.checkedID = $(this).attr("data-id");//用户id
                    window.checkedPID = $(this).attr("data-pid");//父节点id
                    window.zhanghaoName = $(this).siblings(".table-celB").html();//用户账号名
                    window.zhanghaoNC = $(this).siblings(".table-celC").html();//用户昵称
                    window.zhanghaoPSW = $(this).find("i")[0].attributes[0].nodeValue;//用户psw
                    if($(this).find("i").hasClass("i-checked")){
                        $(this).find("i").removeClass("i-checked").addClass("i-checkno").parent(".wantXZ").parent(".tableMainEach").css("background","#fff").siblings().find(".wantXZ").find("i").removeClass("i-checked").addClass("i-checkno").parent(".wantXZ").parent(".tableMainEach").css("background","#fff");
                    }else{
                        $(this).find("i").removeClass("i-checkno").addClass("i-checked").parent(".wantXZ").parent(".tableMainEach").css("background","#F4F4F4").siblings().find(".wantXZ").find("i").removeClass("i-checked").addClass("i-checkno").parent(".wantXZ").parent(".tableMainEach").css("background","#fff");
                    }
                })
                //用户禁用与前面checkbox勾选END

            } 
        });            
         

    })
    var clickSearching = function(curPagenums){
        var name = $(".searchactName").val();
        $.vloading.show();
        $.ajax({ 
            type:"post", 
            url:urls.getAccoutlists, 
            data: {token:token,name:name,pageNow:curPagenums,pageSize:10}, 
            dataType: 'json', 
            async : true,//同步，操作全局变量赋值成功 
            success:function(result){ 
                $.vloading.remove();
                if(result.data.records.length==0){
                    $(".tableMainbtom").html('<p class="txtc" style="padding:19px 0;font-size:16px;border-right:1px solid #e9e9e9;border-bottom:1px solid #e9e9e9;">暂无数据</p>'); 
                }else{
                    var getAllAccoutHtml = '';
                    for(var i=0;i<result.data.records.length;i++){
                        if(result.data.records[i].locked=="1"){
                            getAllAccoutHtml += '<div class="tableMainEach clearfix"><div data-pid="'+result.data.records[i].parentId+'" data-id="'+result.data.records[i].id+'" data-flag="'+result.data.records[i].locked+'" class="wantXZ table-celA fl"><i data-psd="'+result.data.records[i].password+'" class="xwl i-checkno pdr5"></i></div><div class="table-celB fl">'+result.data.records[i].accountName+'</div><div class="table-celC fl">'+result.data.records[i].userName+'</div><div class="table-celD fl">'+result.data.records[i].total+'</div><div class="table-celE fl">'+result.data.records[i].createTime+'</div><div class="table-celF fl"><div data-flag="'+result.data.records[i].locked+'" data-id="'+result.data.records[i].id+'" class="ifqiyong toggleSwitch toggleListSwitchBtns toggleSwitchIDing"><input type="checkbox" checked /><span class="btn"></span><span class="bg"></span></div></div></div>';
                        }else{
                            getAllAccoutHtml += '<div class="tableMainEach clearfix"><div data-pid="'+result.data.records[i].parentId+'" data-id="'+result.data.records[i].id+'" data-flag="'+result.data.records[i].locked+'" class="wantXZ table-celA fl"><i data-psd="'+result.data.records[i].password+'" class="xwl i-checkno pdr5"></i></div><div class="table-celB fl">'+result.data.records[i].accountName+'</div><div class="table-celC fl">'+result.data.records[i].userName+'</div><div class="table-celD fl">'+result.data.records[i].total+'</div><div class="table-celE fl">'+result.data.records[i].createTime+'</div><div class="table-celF fl"><div data-flag="'+result.data.records[i].locked+'" data-id="'+result.data.records[i].id+'" class="ifqiyong toggleSwitch toggleListSwitchBtns toggleSwitchIDing"><input type="checkbox" /><span class="btn"></span><span class="bg"></span></div></div></div>';
                        }
                    }
                    $(".tableMainbtom").html(getAllAccoutHtml);                        
                }
                //总页数
                $(".allpagesNums").html(result.data.pageCount);
                //总记录数
                $(".allRecord").html(result.data.rowCount);

                //用户禁用与前面CheckBox勾选START
                //启用禁用账户
                var ifkaiqiStatus = 0;
                $(".toggleListSwitchBtns").on("click",function(){
                    var id = $(this).attr("data-id");
                    var curBtnStatusCode = $(this).attr("data-flag");
                    if(ifkaiqiStatus==0){
                        if(curBtnStatusCode==1){
                            //触发右边按钮的同时，更新左边checkbox的data-flag状态码
                            $(this).attr("data-flag","0").parent(".table-celF").siblings(".wantXZ").attr("data-flag","0").find("p").html("0");
                        }else{
                            $(this).attr("data-flag","1").parent(".table-celF").siblings(".wantXZ").attr("data-flag","1").find("p").html("1");
                        }
                        ifkaiqiStatus=1;
                    }else{
                        if(curBtnStatusCode==1){
                            $(this).attr("data-flag","0").parent(".table-celF").siblings(".wantXZ").attr("data-flag","0").find("p").html("0");
                        }else{
                            $(this).attr("data-flag","1").parent(".table-celF").siblings(".wantXZ").attr("data-flag","1").find("p").html("1");
                        }
                        ifkaiqiStatus=0;
                    }
                    window.globalSwitchcode = $(this).attr("data-flag");//important perfect!
                    $.post(urls.disabledAccout,{locked:$(this).attr("data-flag"),token:token,id:id}, 
                        function(result){
                            $.vloading.remove();
                            if(result.code == "1"){
                                $.flytip(result.msg);
                            }else{
                                $.flytip(result.msg);
                            }

                    },"json");
                })
                //勾选账号
                //不管是否有点击右边按钮，都同步通讯至左边checkbox状态码
                $(".wantXZ").on("click",function(){
                    window.globalSwitchcode = $(this).attr("data-flag");//important perfect!
                    window.checkedID = $(this).attr("data-id");//用户id
                    window.checkedPID = $(this).attr("data-pid");//父节点id
                    window.zhanghaoName = $(this).siblings(".table-celB").html();//用户账号名
                    window.zhanghaoNC = $(this).siblings(".table-celC").html();//用户昵称
                    window.zhanghaoPSW = $(this).find("i")[0].attributes[0].nodeValue;//用户psw
                    if($(this).find("i").hasClass("i-checked")){
                        $(this).find("i").removeClass("i-checked").addClass("i-checkno").parent(".wantXZ").parent(".tableMainEach").css("background","#fff").siblings().find(".wantXZ").find("i").removeClass("i-checked").addClass("i-checkno").parent(".wantXZ").parent(".tableMainEach").css("background","#fff");
                    }else{
                        $(this).find("i").removeClass("i-checkno").addClass("i-checked").parent(".wantXZ").parent(".tableMainEach").css("background","#F4F4F4").siblings().find(".wantXZ").find("i").removeClass("i-checked").addClass("i-checkno").parent(".wantXZ").parent(".tableMainEach").css("background","#fff");
                    }
                })
                //用户禁用与前面checkbox勾选END

            } 
        });               
    }


    //个人中心下拉
    $("#showmyAccountMenu").toggle(
        function(){$("#toggleshowmenu").show();},
        function(){$("#toggleshowmenu").hide();}
    );
    //弹出层
    $(".closeLayerbox,.cancelBtning").on("click",function(){
        $(".layerBoxing,.layerBoxing2,.masking,.deviceAllocationLayer").hide();
    })










//事件监听分页触发时，获得当前页码数
pageNav.pre="上一页";
pageNav.next="下一页";


//$.flytip("zhowdom");$.vloading.show();$.vloading.remove();





//req.js end
})